

  const express = require('express')
  const app = express()
  const router = express.Router()
  const mysql = require('../增删改查/mysql')

  app.use(router)

  router.get('/', (req, res) => {
    res.send('首页')
  })

  router.get('/add', (req, res) => {
    const stu = { sno: '1940233101', name: '张三' }
    const query = `insert into student (sno, name) VALUES (?, ?)`
    mysql.query(query, [stu.sno, stu.name], (err, result) => {
      if(err) {
        console.log(err.message)
        res.send('插入失败' + err.message)
      }
      console.log(result)
      res.send('插入成功')
    })
  })

  router.get('/delete', (req, res) => {
    const query = `delete from student where sno = ?`
    mysql.query(query, '1940233101', (err, result) => {
      if(err) {
        console.log(err.message)
        res.send('删除失败' + err.message)
      }
      console.log(result)
      res.send('删除成功')
    })
  })

  router.get('/update', (req, res) => {
    const stu = {sno: '1940233104', name: '李四'}
    const query = `update student set ? where sno = ?`
    mysql.query(query, [stu, stu.sno], (err, result) => {
      if(err) {
        console.log(err.message)
        res.send('修改失败' + err.message)
      }
      console.log(result)
      res.send('修改成功')
    })
  })

  router.get('/query', (req, res) => {
    const query = `select * from student`
    mysql.query(query, (err, result) => {
      if(err) {
        console.log(err.message)
        res.send('查询失败' + err.message)
      }
      console.log(result)
      res.send('查询成功')
    })
  })

  app.listen(8080, () => {
    console.log("Server is running at http://127.0.0.1:8080")
  })